package com.weijian.controller;

import com.weijian.common.ApiResponse;
import com.weijian.dal.dto.order.CancelOrderRequest;
import com.weijian.dal.dto.order.CreateOrderRequest;
import com.weijian.dal.dto.order.PayOrderRequest;
import com.weijian.dal.entity.Order;
import com.weijian.dal.vo.OrderVO;
import com.weijian.service.OrderService;
import com.weijian.util.SessionUserUtil;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.validation.Valid;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 订单控制器
 * 处理订单相关的HTTP请求
 *
 * @author weijian
 */
@RestController
@RequestMapping("/api/order")
@Tag(name = "订单管理", description = "订单相关的API接口，包括创建订单、支付、取消等操作")
public class OrderController {

    @Resource
    private OrderService orderService;

    /**
     * 创建订单
     *
     * @param request 创建订单请求
     * @return 创建的订单信息
     */
    @PostMapping("/create")
    @Operation(summary = "创建订单", description = "根据课程ID创建新的订单记录")
    public ApiResponse<Order> createOrder(@Valid @RequestBody CreateOrderRequest request) {
        Long userId = SessionUserUtil.getCurrentUserId();
        Order order = orderService.createOrder(userId, request.getCourseId());
        return ApiResponse.success(order);
    }

    /**
     * 查询用户订单列表
     *
     * @return 订单列表
     */
    @GetMapping("/list")
    @Operation(summary = "查询用户订单列表", description = "获取当前登录用户的所有订单信息")
    public ApiResponse<List<OrderVO>> getUserOrders() {
        Long userId = SessionUserUtil.getCurrentUserId();
        List<OrderVO> orders = orderService.getUserOrders(userId);
        return ApiResponse.success(orders);
    }

    /**
     * 订单支付
     *
     * @param request 支付订单请求
     * @return 支付结果
     */
    @PostMapping("/pay")
    @Operation(summary = "订单支付", description = "对指定订单进行支付操作")
    public ApiResponse<Void> payOrder(@Valid @RequestBody PayOrderRequest request) {
        Long userId = SessionUserUtil.getCurrentUserId();
        orderService.payOrder(request.getOrderId(), userId, request.getPaymentMethod());
        return ApiResponse.success(null);
    }

    /**
     * 取消订单
     *
     * @param request 取消订单请求
     * @return 取消结果
     */
    @PostMapping("/cancel")
    @Operation(summary = "取消订单", description = "取消未支付的订单")
    public ApiResponse<Void> cancelOrder(@Valid @RequestBody CancelOrderRequest request) {
        Long userId = SessionUserUtil.getCurrentUserId();
        orderService.cancelOrder(request.getOrderId(), userId);
        return ApiResponse.success(null);
    }

    /**
     * 初始化课程库存到Redis（开发测试用）
     *
     * @param courseId 课程ID
     * @param stock    库存数量
     * @return 初始化结果
     */
    @PostMapping("/init-stock")
    @Operation(summary = "初始化课程库存", description = "开发测试用：初始化指定课程的库存到Redis缓存")
    public ApiResponse<Void> initCourseStock(
            @Parameter(description = "课程ID", example = "1", required = true) 
            @RequestParam Long courseId,
            @Parameter(description = "库存数量", example = "100", required = true) 
            @RequestParam Integer stock) {
        orderService.initCourseStock(courseId, stock);
        return ApiResponse.success(null);
    }
}